package com.bluefeather.songlyric;

import com.bluefeather.songlyric.DB.MUSIC;

import android.app.ListFragment;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MusicList extends ListFragment{
	
	public static String	TAG	= "MUSIC";
	
	private String			_where = null;
	private String[]		_columns = new String[] {  DB.MUSIC._NAME };
	private int[]			_ids = new int[] { R.id.music_name };
	private int				_currentPage = 0;
	private int				_totalPages = 0;
	private int[]			_pageFirst;
	
	static MusicList newInstance(int menuId) {
		MusicList ml = new MusicList();
		Bundle args = new Bundle();
		String where = menuId == R.string.drawer_short_music ? MUSIC._SIZE + " = 's'" :
			menuId == R.string.drawer_mediate_music ? MUSIC._SIZE + " = 'm'" :
			menuId == R.string.drawer_long_music ? MUSIC._SIZE + " = 'l'" :
			menuId == R.string.drawer_ping_tone ? MUSIC._TONE_TYPE + " = " + DB.TONE_TYPE_P :
			menuId == R.string.drawer_ze_tone ? MUSIC._TONE_TYPE + " = " + DB.TONE_TYPE_Z :
			menuId == R.string.drawer_change_tone ? MUSIC._TONE_TYPE + " = " + DB.TONE_TYPE_C :
			menuId == R.string.drawer_favorite_music ? MUSIC._FAVORIT + " > 0 " : null;
		args.putString("WHERE", where);
		ml.setArguments(args);
		return ml;
	}
	
	public MusicList() {}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		_where = getArguments() != null ? getArguments().getString("WHERE") : null;
		_totalPages = DB.instance().countMusicPages(_where);
		if(_totalPages == 0)
			_totalPages = 1;
		_pageFirst = new int[_totalPages + 1];
		_pageFirst[0] = 0;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args) {
		View view = inflater.inflate(R.layout.fragment_music_list, container, false);
		view.setBackgroundColor(0xffffffff);
		Cursor c = DB.instance().getMusicList(_where, 0);
		c.moveToLast();
		if(c.getCount() > 0)
			_pageFirst[_currentPage + 1] = c.getInt(c.getColumnIndex(MUSIC._ID));
		c.moveToFirst();
		SongLyricActivity activity = (SongLyricActivity)getActivity();
		activity.setPageInTitle(_currentPage);
		setListAdapter(new SimpleCursorAdapter(getActivity(), R.layout.row_music, c, _columns, _ids, 0));
		return view;
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		SongLyricActivity activity = (SongLyricActivity)getActivity();
		activity.openMusic((int)id);
	}
	
	public void goPrev() {
		if(_currentPage > 0) {
			_currentPage--;
			Cursor c = DB.instance().getMusicList(_where, _pageFirst[_currentPage]);
			SongLyricActivity activity = (SongLyricActivity)getActivity();
			activity.setPageInTitle(_currentPage);
			setListAdapter(new SimpleCursorAdapter(getActivity(), R.layout.row_music, c, _columns, _ids, 0));
		} else {
			SongLyricApp.instance().showToast(R.string.warning_first_page);
			return;
		}
	}
	
	public void goNext() {
		if(_currentPage < _totalPages - 1) {
			_currentPage++;
			Cursor c = DB.instance().getMusicList(_where, _pageFirst[_currentPage]);
			c.moveToLast();
			_pageFirst[_currentPage + 1] = c.getInt(c.getColumnIndex(MUSIC._ID));
			c.moveToFirst();
			SongLyricActivity activity = (SongLyricActivity)getActivity();
			activity.setPageInTitle(_currentPage);
			setListAdapter(new SimpleCursorAdapter(getActivity(), R.layout.row_music, c, _columns, _ids, 0));
		} else {
			SongLyricApp.instance().showToast(R.string.warning_last_page);
			return;
		}
	}

	public void searchMusic(String name) {
		Cursor c = DB.instance().getMusicList(MUSIC._NAME + " like '%" + name + "%'", 0);
		setListAdapter(new SimpleCursorAdapter(getActivity(), R.layout.row_music, c, _columns, _ids, 0));
	}
}
